package com.smmisia.article.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smmisia.article.dto.address.AddressDTO;
import com.smmisia.article.dto.address.UpdateAddressDTO;
import com.smmisia.article.entity.Address;
import com.smmisia.article.entity.SysRegion;
import com.smmisia.article.exception.customException;
import com.smmisia.article.mapper.AddressMapper;
import com.smmisia.article.mapper.SysRegionMapper;
import com.smmisia.article.service.AddressService;
import com.smmisia.article.utils.BeanUtil;
import com.smmisia.article.utils.LoginUtil;
import com.smmisia.common.Result.Result;
import com.smmisia.common.enums.DeletedEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address>
    implements AddressService{

    @Autowired
    protected SysRegionMapper sysRegionMapper;

    @Autowired
    protected AddressMapper addressMapper;

    /**
     * 获取一级省市
     * @return
     */
    @Override
    public Result<Map<Integer,String>> getProvince() {
        LambdaQueryWrapper<SysRegion> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysRegion::getRegionParentId, -1);
        List<SysRegion> sysRegions = sysRegionMapper.selectList(wrapper);
        Map<Integer, String> map = new HashMap<Integer, String>();
        if (sysRegions != null) {
            sysRegions.stream()
                    .forEach(province -> map.put(Integer.valueOf(province.getRegionId()), province.getRegionName()));
            return Result.ok(map);
        }
        return Result.fail("获取失败");
    }

    /**
     * 获取二级省市
     * @return
     */
    @Override
    public Result<Map<Integer, String>> getCity(Integer provinceId) {
        LambdaQueryWrapper<SysRegion> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysRegion::getRegionParentId, provinceId);
        List<SysRegion> sysRegions = sysRegionMapper.selectList(wrapper);
        Map<Integer, String> map = new HashMap<Integer, String>();
        if (sysRegions != null) {
            sysRegions.stream()
                    .forEach(province -> map.put(Integer.valueOf(province.getRegionId()), province.getRegionName()));
            return Result.ok(map);
        }
        return Result.fail("获取失败");
    }

    /**
     * 获取三级省市
     * @return
     */
    @Override
    public Result<Map<Integer, String>> getCounty(Integer provinceId) {
        LambdaQueryWrapper<SysRegion> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysRegion::getRegionParentId, provinceId);
        List<SysRegion> sysRegions = sysRegionMapper.selectList(wrapper);
        Map<Integer, String> map = new HashMap<Integer, String>();
        if (sysRegions != null) {
            sysRegions.stream()
                    .forEach(province -> map.put(Integer.valueOf(province.getRegionId()), province.getRegionName()));
            return Result.ok(map);
        }
        return Result.fail("获取失败");
    }

    /**
     * 添加收货地址
     * @param addressDTO
     * @return
     */
    @Override
    public Result<Address> add(AddressDTO addressDTO) {
        Address address = BeanUtil.convert(addressDTO, Address.class);
        address.setCreatedTime(LocalDateTime.now());
        address.setUpdatedTime(LocalDateTime.now());
        address.setDeleted(DeletedEnum.IS_UNDELETED.getCode());
        int insert = addressMapper.insert(address);
        Assert.isTrue(insert ==1 ,()->new customException("添加收货地址失败"));
        return Result.ok(address);
    }

    /**
     * 删除收货地址
     * @param id
     * @return
     */
    @Override
    public Result<String> delete(Integer id) {
        Integer loginId = (Integer) StpUtil.getLoginId();
        LambdaQueryWrapper<Address> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Address::getId, id);
        wrapper.eq(Address::getUserId, loginId);
        int delete = addressMapper.delete(wrapper);
        Assert.isTrue(delete == 1,()->new customException("删除失败"));
        return Result.ok("删除成功");
    }

    /**
     * 更新收货地址
     * @param updateAddressDTO
     * @return
     */
    @Override
    public Result<Address> updateAddress(UpdateAddressDTO updateAddressDTO) {
        Address address = BeanUtil.convert(updateAddressDTO, Address.class);
        address.setUpdatedTime(LocalDateTime.now());
        int result = addressMapper.updateById(address);
        Assert.isTrue(result == 1,()->new customException("更新失败"));
        return Result.ok(address);
    }

    /**
     * 获取我的收货地址
     * @return
     */
    @Override
    public Result<List<Address>> getInfo() {
        Integer loginId = Integer.valueOf(LoginUtil.getLoginId());
        LambdaQueryWrapper<Address> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Address::getUserId, loginId);
        List<Address> addresses = addressMapper.selectList(wrapper);
        return Result.ok(addresses);
    }
}




